/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/** *****************************************************************************************************
 * \file     Mcu_Xtal.c                                                                                 *
 * \brief    AUTOSAR 4.3.1 MCAL Mcu Driver                                                              *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2023/08/03     <td>1.0.0                                                                     *
 * </table>                                                                                             *
 *******************************************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif
/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Mcu_Xtal.h"
#include "Mcu_Fault.h"
#include "Xtal_Reg.h"
#include "Mcal_Delay.h"
#include "__regs_base.h"
#include "Mcu_GeneralTypes.h"

/********************************************************************************************************
 *                                 Private Macro definition                                             *
 *******************************************************************************************************/
#define FS24M_WAIT_TIME                   100U
/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/
#define MCU_START_SEC_CODE
#include "Mcu_MemMap.h"

/** *****************************************************************************************************
 * \brief This function switch FS32K from RC to xtal.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_Fs32KTrySwitchToXtal(void)
 *
 * Service ID[hex]    : /
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : None
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : ErrId
 *
 * Description        : This function switch FS32K from RC to xtal.
 * \endverbatim
 * Traceability       : SW_SM005
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_Fs32KTrySwitchToXtal(void)
{
    Std_ReturnType retVal = MCU_E_TIMEOUT;
    uint32 timeOutCounts = 100U, count = 0U;
    uint32 v;
    boolean flag = FALSE;
    uint32 base = APB_FS_32K_BASE;

    v = readl(base + FS_GLB_CTL_OFF);

    McuFaultInj_Mcu_Ip_Fs32KTrySwitchToXtal_01();

    /* check xtal ready */
    if (0U != (v & BM_FS_GLB_CTL_XTAL_RDY))
    {
        /* [1:1] 0:from osc32K
         *       1:from xtal
         */
        v |= BM_FS_GLB_CTL_FS_SRC_SEL;

        writel(v, base + FS_GLB_CTL_OFF);

        /* wait for xtal active, 32K clock 3 cycle */
        while (count < timeOutCounts)
        {
            retVal = Mcal_udelay(100U);

            if (E_OK != retVal)
            {
                flag = TRUE;
            }
            else
            {
                v = readl(base + FS_GLB_CTL_OFF);
                McuFaultInj_Mcu_Ip_Fs32KTrySwitchToXtal_03();
                if (0U != (v & BM_FS_GLB_CTL_XTAL_ACTIVE))
                {
                    flag = TRUE;
                }
                else
                {
                    count++;
                }
            }

            if (TRUE == flag)
            {
                break;
            } /* else not needed */
        }

        McuFaultInj_Mcu_Ip_Fs32KTrySwitchToXtal_02();

        if (FALSE == flag)
        {
            retVal = MCU_E_TIMEOUT;
        }
    } /* else not needed */

    /* will read to confirm that the register has been reached */
    (void)readl(base + FS_GLB_CTL_OFF);

    return retVal;
}

/** *****************************************************************************************************
 * \brief This function switch FS32K from xtal to RC.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_Fs32KTrySwitchToRC(void)
 *
 * Service ID[hex]    : /
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : None
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : ErrId
 *
 * Description        : This function switch FS32K from xtal to RC.
 * \endverbatim
 * Traceability       : SW_SM005
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_Fs32KTrySwitchToRC(void)
{
    Std_ReturnType retVal = MCU_E_TIMEOUT;
    uint32 timeOutCounts = 100U, count = 0U;
    uint32 v;
    boolean flag = FALSE;
    uint32 base = APB_FS_32K_BASE;

    v = readl(base + FS_GLB_CTL_OFF);

    McuFaultInj_Mcu_Ip_Fs32KTrySwitchToRC_01();

    /* check xtal ready */
    if (0U != (v & BM_FS_GLB_CTL_RC_RDY))
    {
        /* [1:1] 0:from osc32K
         *       1:from xtal
         */
        v &= ~BM_FS_GLB_CTL_FS_SRC_SEL;

        writel(v, base + FS_GLB_CTL_OFF);

        /* wait for xtal active, 32K clock 3 cycle */
        while (count < timeOutCounts)
        {
            retVal = Mcal_udelay(100U);

            if (E_OK != retVal)
            {
                flag = TRUE;
            }
            else
            {
                v = readl(base + FS_GLB_CTL_OFF);
                McuFaultInj_Mcu_Ip_Fs32KTrySwitchToRC_03();
                if (0U != (v & BM_FS_GLB_CTL_RC_ACTIVE))
                {
                    flag = TRUE;
                }
                else
                {
                    count++;
                }
            }

            if (TRUE == flag)
            {
                break;
            } /* else not needed */
        }

        McuFaultInj_Mcu_Ip_Fs32KTrySwitchToRC_02();

        if (FALSE == flag)
        {
            retVal = MCU_E_TIMEOUT;
        }
    } /* else not needed */

    return retVal;
}


/** *****************************************************************************************************
 * \brief This function switch FS24M from RC to xtal.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_Fs24MTrySwitchToXtal(void)
 *
 * Service ID[hex]    : /
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : None
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : ErrId
 *
 * Description        : This function switch FS24M from RC to xtal.
 * \endverbatim
 * Traceability       : SW_SM005
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_Fs24MTrySwitchToXtal(void)
{
    Std_ReturnType retVal = E_OK;
    uint32 v;
    uint32 base = APB_FS_24M_BASE;

    v = readl(base + FS_GLB_CTL_OFF);

    McuFaultInj_Mcu_Ip_Fs24MTrySwitchToXtal_01();

    /* check xtal ready */
    if (0U != (v & BM_FS_GLB_CTL_XTAL_RDY))
    {
        /* [1:1] 0:from osc32K
         *       1:from xtal
         */
        v |= BM_FS_GLB_CTL_FS_SRC_SEL;

        writel(v, base + FS_GLB_CTL_OFF);

        /* wait for xtal active */
        if (FALSE == Mcu_Ip_WaitForBitTimes(base + FS_GLB_CTL_OFF, BM_FS_GLB_CTL_XTAL_ACTIVE, 1U,
                                            FS24M_WAIT_TIME))
        {
            retVal = MCU_E_TIMEOUT;
        } /* else not needed */
    }
    else
    {
        retVal = MCU_E_TIMEOUT;
    }

    return retVal;
}

/** *****************************************************************************************************
 * \brief This function switch FS24M from xtal to RC.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcu_Ip_Fs24MTrySwitchToRC(void)
 *
 * Service ID[hex]    : /
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : None
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : ErrId
 *
 * Description        : This function switch FS24M from xtal to RC.
 * \endverbatim
 * Traceability       : SW_SM005
 *******************************************************************************************************/
Std_ReturnType Mcu_Ip_Fs24MTrySwitchToRC(void)
{
    Std_ReturnType retVal = E_OK;
    uint32 v;
    uint32 base = APB_FS_24M_BASE;

    v = readl(base + FS_GLB_CTL_OFF);

    McuFaultInj_Mcu_Ip_Fs24MTrySwitchToRC_01();

    /* check xtal ready */
    if (0U != (v & BM_FS_GLB_CTL_RC_RDY))
    {
        /* [1:1] 0:from osc32K
         *       1:from xtal
         */
        v &= ~BM_FS_GLB_CTL_FS_SRC_SEL;

        writel(v, base + FS_GLB_CTL_OFF);

        /* wait for rc active */
        if (FALSE == Mcu_Ip_WaitForBitTimes(base + FS_GLB_CTL_OFF, BM_FS_GLB_CTL_RC_ACTIVE, 1U,
                                            FS24M_WAIT_TIME))
        {
            retVal = MCU_E_TIMEOUT;
        } /* else not needed */
    }
    else
    {
        retVal = MCU_E_TIMEOUT;
    }

    return retVal;
}
#define MCU_STOP_SEC_CODE
#include "Mcu_MemMap.h"

#ifdef __cplusplus
}
#endif
/* End of file */
